package com.coder.bike.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coder.bike.entity.Customer;
import com.coder.bike.entity.Staff;
import com.coder.bike.mapper.CustomerMapper;
import com.coder.bike.service.ICustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 沈诩宸
 * @since 2025-04-22
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {
    @Override
    public Customer selectByUsername(String username) {
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public Page<Customer> searchByPage(Page<Customer> page, Customer customer) {
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();

        // 账户信息查询
        queryWrapper.like(StrUtil.isNotBlank(customer.getUsername()), "username", customer.getUsername());
        queryWrapper.like(StrUtil.isNotBlank(customer.getName()), "name", customer.getName());
        queryWrapper.like(StrUtil.isNotBlank(customer.getNickname()), "nickname", customer.getNickname());

        // 联系方式查询
        queryWrapper.like(StrUtil.isNotBlank(customer.getTel()), "tel", customer.getTel());
        queryWrapper.like(StrUtil.isNotBlank(customer.getEmail()), "email", customer.getEmail());

        // 状态查询（只有当status不为null时才添加条件）
        if (customer.getStatus() != null) {
            queryWrapper.eq("status", customer.getStatus());
        }
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public Customer selectByName(String realName) {
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", realName);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public Customer selectByPhone(String tel) {
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tel",  tel);
        return baseMapper.selectOne(queryWrapper);
    }

}
